const multer = require('multer')
const fs = require('fs')
const path = require("path")

/**
 * 上传文件
 */
exports.uploadFile = [multer({
    dest: 'public/upload/file',
    limits: {
        fileSize: 8000 * 1000,
        files: 5
    }, filename: function (req, file, cb) {
        var changedName = (new Date().getTime()) + '-' + file.originalname;
        cb(null, changedName);
    }, onError: function (err, next) {
        if (err) {
            return res.send({
                code: 500,
                msg: '文件上传发送了错误！' + err.message,
                data: {}
            })
        } else {
            next(err);
        }
        next();
    }
}).any(), (req, res) => {
    // console.log('Application===>', Application)
    const ip = req.ip
    console.log(ip + '上传文件')
    // console.log('文件：' + req.files[0])
    let url = ''
    if (null == req.files[0]) {
        url = '无'
        return res.send({
            code: 500,
            msg: '没有文件',
            data: { url }
        })
    }
    else {
        console.log(req.files[0])
        if (req.files.length > 1) {
            for (let i = 0; i < req.files.length; i++) {
                let oldName = req.files[i].path;//获取名字
                //给新名字加上原来的后缀
                let newName = req.files[i].path + path.parse(req.files[i].originalname).ext;
                fs.renameSync(oldName, newName);//改图片的名字
                if (i != req.files.length - 1) url = url + "http://localhost:8081/upload/file/" + req.files[i].filename + path.parse(req.files[i].originalname).ext + ','
                else url = url + "http://localhost:8081/upload/file/" + req.files[i].filename + path.parse(req.files[i].originalname).ext
            }
        } else {
            // console.log(req.files)
            let oldName = req.files[0].path;//获取名字
            //给新名字加上原来的后缀
            let newName = req.files[0].path + path.parse(req.files[0].originalname).ext;
            fs.renameSync(oldName, newName);//改图片的名字
            url = "http://localhost:8081/upload/file/" + req.files[0].filename + path.parse(req.files[0].originalname).ext
        }
        return res.send({
            code: 200,
            msg: '上传成功',
            data: { url }
        })
    }
}
]

/**
 * 上传图片
 */
exports.uploadImage = [multer({
    dest: 'public/upload/images',
    limits: {
        fileSize: 8000 * 1000,
        files: 5
    }, filename: function (req, file, cb) {
        var changedName = (new Date().getTime()) + '-' + file.originalname;
        cb(null, changedName);
    }, onError: function (err, next) {
        if (err) {
            return res.send({
                code: 500,
                msg: '文件上传发送了错误！' + err.message,
                data: {}
            })
        } else {
            next(err);
        }
        next();
    }
}).any(), (req, res) => {
    const ip = req.ip
    console.log(ip + '上传文件')
    let url = '';
    // console.log('文件：' + req.files[0])
    if (null == req.files[0]) url = '无'
    else {
        if (req.files.length > 1) {
            // console.log(req.files)
            for (let i = 0; i < req.files.length; i++) {
                // console.log(req.files[i].mimetype, req.files[i].fieldname)
                let oldName = req.files[i].path;//获取名字
                //给新名字加上原来的后缀
                let newName = req.files[i].path + path.parse(req.files[i].originalname).ext;
                fs.renameSync(oldName, newName);//改图片的名字
                if (i != req.files.length - 1) url = url + "http://localhost:8081/upload/images/" + req.files[i].filename + path.parse(req.files[i].originalname).ext + ','
                else url = url + "http://localhost:8081/upload/images/" + req.files[i].filename + path.parse(req.files[i].originalname).ext
            }
        } else {
            // console.log(req.files)
            let oldName = req.files[0].path;//获取名字
            //给新名字加上原来的后缀
            let newName = req.files[0].path + path.parse(req.files[0].originalname).ext;
            fs.renameSync(oldName, newName);//改图片的名字
            url = "http://localhost:8081/upload/images/" + req.files[0].filename + path.parse(req.files[0].originalname).ext//该图片的预览路径
        }
    }
    console.log(url)
    return res.send({
        code: 200,
        msg: '上传成功',
        data: { url }
    })

}
]